package com.yuwj.chinaa.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;

import com.yuwj.chinaa.R;
import com.yuwj.chinaa.base.BaseActivity;
import com.yuwj.chinaa.presenter.IPresenter;
import com.yuwj.chinaa.presenter.presenterImpl.PresenterImpl;
import com.yuwj.chinaa.util.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;

/**
 * 反馈页面
 */
public class FeedbackActivity extends BaseActivity {

    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    IPresenter presenter;

    public static Intent createIntent(Context context) {
        Intent intent = new Intent(context, FeedbackActivity.class);
        return intent;
    }


    @Override
    public int setCustomContentViewResourceId() {
        return R.layout.activity_feedback;
    }

    @Override
    public void initParam() {
        presenter = new PresenterImpl(this);
    }

    @Override
    public void initView() {
        initActionBar(toolbar, tvTitle, "用户反馈");

    }

    @Override
    public void initOperator() {
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_feedback, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        switch (item.getItemId()) {
            case R.id.submit:
                String content = etContent.getText().toString().trim();
                if (TextUtils.isEmpty(content)) {
                    ToastUtil.getInstance().shortLength(context, "请输入要反馈的问题");
                } else {
                    JSONObject param = new JSONObject();
                    try {
                        param.put("content", content);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    presenter.appFeedback(param.toString());
                }

                break;
            case android.R.id.home:
                finish();
                break;
        }
        return true;
    }

    @Override
    public void onAppFeedbackSuccess(String info) {
        super.onAppFeedbackSuccess(info);
        ToastUtil.getInstance().shortLength(context, "反馈成功");
        FeedbackActivity.this.finish();
    }

    @Override
    public void onAppFeedbackFail(Exception e) {
        super.onAppFeedbackFail(e);
        ToastUtil.getInstance().shortLength(context, "" + e.getMessage());
    }
}
